package com.tandn;

import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import com.tandn.helpers.XMLParserHandler;
import com.tandn.models.ATM;

import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class AppMain extends Application
{
	public static XMLParserHandler _XmlParserHandler = new XMLParserHandler();

	public static String _bankName = null;
	public static int _numberSearchATMWithBank = 0;
	public static List<ATM> _listATMSearchWithBank = null;
	public static int _numberSearchATMWithCity = 0;
	public static List<ATM> _listATMSearchWithCity = null;
	public static int _numberSearchATMWithDistrict = 0;
	public static List<ATM> _listATMSearchWithDistrict = null;
	
	public AppMain()
	{
		new Thread(new Runnable() {

			public void run()
			{
				try
				{
					AppMain.readXMLData(getAssets().open("data.xml"));
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}).start();
	}

	public static void loadData()
	{}
	
	public static boolean isNetworkAvailable(Context context)
	{
		final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo != null && networkInfo.isAvailable()
				&& networkInfo.isConnected())
			return true;
		else
			return false;
	}

	public static void readXMLData(InputStream inputStream) throws Exception
	{
		/* Initiate the XML reader */
		SAXParserFactory parserFactory = SAXParserFactory.newInstance();
		SAXParser parser = parserFactory.newSAXParser();
		XMLReader xmlReader = parser.getXMLReader();
		xmlReader.setContentHandler(_XmlParserHandler);

		/* Using the XML reader parsing XML from an input stream */
		xmlReader.parse(new InputSource(inputStream));
	}

	public static void readXMLData(String fileName) throws Exception
	{
		/* Initiate the XML reader */
		SAXParserFactory parserFactory = SAXParserFactory.newInstance();
		SAXParser parser = parserFactory.newSAXParser();
		XMLReader xmlReader = parser.getXMLReader();
		xmlReader.setContentHandler(_XmlParserHandler);

		/* Using the XML reader parsing XML from an input stream */
		xmlReader.parse(new InputSource(new FileReader(new File(fileName))));
	}
	
	public static void showMessageBox(Context context, String message)
	{
		Toast.makeText(context, message, Toast.LENGTH_LONG);
	}

}
